package com.a16.yuejian.yuejian.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.a16.yuejian.yuejian.R;
import com.a16.yuejian.yuejian.javabean.Login;
import com.a16.yuejian.yuejian.listener.LoginListener;
import com.a16.yuejian.yuejian.model.UserModel;


public class LoginActivity extends AppCompatActivity implements View.OnClickListener,LoginListener{
    private EditText phone,pwd;
    private Button login;
    private TextView reg;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private CheckBox checkBox;
    boolean flag = false ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
        initEvents();
        remember();
    }

    private void remember() {
        if(sp.getBoolean("remember",false)){
            checkBox.setChecked(true);
            phone.setText(sp.getString("phone",""));
            pwd.setText(sp.getString("pwd",""));
        }else {
            checkBox.setChecked(false);
        }
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    editor.putBoolean("remember", true);
                    editor.putString("phone",phone.getText().toString());
                    editor.putString("pwd",pwd.getText().toString());
                }else {
                    editor.putBoolean("remember",false);
                }
                editor.commit();
            }
        });
    }

    private void initEvents() {
        login.setOnClickListener(this);
        reg.setOnClickListener(this);
        pwd.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Drawable drawable = pwd.getCompoundDrawables()[2];
                if (drawable==null){
                    return false;
                }else if(event.getAction() != MotionEvent.ACTION_UP){
                    return false;
                } else if (event.getX() > pwd.getWidth() - pwd.getPaddingRight() - drawable.getIntrinsicWidth()){
                    if (flag){
                        pwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                        flag=false;
                    }else {
                        pwd.setInputType( InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD );
                        flag=true;
                    }
                }
                return false;
            }
        });
    }

    private void initViews() {
        phone =(EditText)findViewById(R.id.login_phone);
        pwd =(EditText)findViewById(R.id.login_pwd);
        login =(Button)findViewById(R.id.login);
        reg =(TextView)findViewById(R.id.login_reg);
        checkBox = (CheckBox)findViewById(R.id.checkBox);
        sp = getSharedPreferences("userinfo", Context.MODE_PRIVATE);
        editor = sp.edit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login:
                if (phone.getText().toString()!=null&&"".equals(phone.getText().toString())){
                    Toast.makeText(LoginActivity.this,"请输入手机号！", Toast.LENGTH_SHORT).show();
                }else if(pwd.getText().toString()!=null&&"".equals(pwd.getText().toString())){
                    Toast.makeText(LoginActivity.this,"请输入密码！", Toast.LENGTH_SHORT).show();
                }else {
                    UserModel userModel=new UserModel();
                    userModel.login(phone.getText().toString(),pwd.getText().toString(),this);
                }
                break;
            case R.id.login_reg:
                Intent intent = new Intent(LoginActivity.this,RegActivity.class);
                startActivity(intent);
                break;
        }
    }

    public  void saveID(String user_id){
        editor.putString("uid", user_id);
        editor.commit();
    }

    @Override
    public void onResponse(Login uid) {
        if(uid.getUserid().equals("0")){
            Toast.makeText(LoginActivity.this,"手机号或密码错误", Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(LoginActivity.this,"登陆成功", Toast.LENGTH_SHORT).show();
            saveID(uid.getUserid());
            Intent intent = new Intent(LoginActivity.this,MainActivity.class);
            startActivity(intent);
            finish();
        }
    }

    @Override
    public void onFail() {

    }
}
